package rent;

import book.RentBook;
import common.DecF;
import reader.VIPReadernew;

import java.util.Calendar;

// 租书记录类
class RentRecord {
    private String bookNo;      // 图书入库编号
    private int readerID;       // 读者编号
    private String rentDate;    // 租书日期
    private String returnDate;  // 还书日期
    private double rents;       // 租金
    private int bonusPoints;    // 积分

    public RentRecord(RentBook rb, VIPReadernew reader, String rentd) {
        this.bookNo = rb.getBookNo();
        this.readerID = reader.getReaderID();
        rentDate = rentd;
        returnDate = "";
        rents = 0;
        bonusPoints = 0;
    }

    public RentRecord(RentBook rb, VIPReadernew reader, String rentd, String returnd) {
        this(rb, reader, rentd);
        returnDate = returnd;
        bonusPoints = getBonusPoints();
    }

    public int getReaderID() {
        return readerID;
    }

    public String getBookNo() {
        return bookNo;
    }

    public void setRentDate(String rentdate) {
        this.rentDate = rentdate;
    }

    public void setReturnDate(String returndate) {
        this.returnDate = returndate;
    }

    public long sumRentdays() {  // 计算租阅天数
        SumDays sd = new SumDays();
        return sd.setRentDays(rentDate, returnDate);
    }

    public void setRent(int deadTime, double nRent, double dRent, double percent) {
        int rentDays = (int) sumRentdays();
        if (rentDays <= deadTime) {
            rents = DecF.DecD(rentDays * nRent * percent);  // 正常费率
        } else {
            rents = DecF.DecD(((rentDays - deadTime) * dRent + deadTime * nRent) * percent);  // 超期费率
        }
    }

    public double getRent() {
        return rents;
    }

    public int getBonusPoints() {
        bonusPoints += (int) sumRentdays();  // 按天数累计积分
        return bonusPoints;
    }

    public String toString() {
        if (returnDate.length() == 0) {  // 借书记录
            return "入库号:" + getBookNo() + " 租借阅者:" + getReaderID() + " 租书日期:" + rentDate;
        } else {  // 还书记录
            return "入库号:" + getBookNo() + " 租借阅者:" + getReaderID() + " 租书日期:" + rentDate +
                    " 还书日期:" + returnDate + " 租阅天数:" + sumRentdays() + " 积分:" +
                    getBonusPoints() + " 租金:" + getRent();
        }
    }

    // 计算租阅天数的内部类
    private class SumDays {
        Calendar c = Calendar.getInstance();

        public long setRentDays(String rentD, String sendD) {
            int y1 = Integer.parseInt(rentD.substring(0, 4));  // 提取年份
            int m1 = Integer.parseInt(rentD.substring(4, 6));  // 提取月份
            int d1 = Integer.parseInt(rentD.substring(6, 8));  // 提取日子
            c.set(y1, m1, d1);
            long getDate = c.getTimeInMillis();  // 转为毫秒

            int y2 = Integer.parseInt(sendD.substring(0, 4));
            int m2 = Integer.parseInt(sendD.substring(4, 6));
            int d2 = Integer.parseInt(sendD.substring(6, 8));
            c.set(y2, m2, d2);
            long sendDate = c.getTimeInMillis();

            return (sendDate - getDate) / (1000 * 60 * 60 * 24);  // 计算相差天数
        }
    }
}
